﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Web.UI.DataVisualization.Charting;
using System.Drawing;
using HZ.Supervise.Model;
using HZ.Supervise.Model.Enum;

namespace HZ.Supervise.Web.Secondphase.Statistics
{
    public partial class StatisticsAeraByMonth : BasePage
    {
        DataTable dt = new DataTable();
        public string AreaId;
        int year;
        int month;


        public StatisticsAeraByMonth()
        {
            this.ModuleNo = Module.报表统计.ToString();
            this.PageOperateType = OperateType.地区月报.ToString();
        }

        protected void Page_Load(object sender, EventArgs e)
        {
            AreaId = Request["Y"];
            if (!IsPostBack)
            {
                //判断dropdownlist是否当前年数并且显示  by张雄  2011.4.28   create
                year = DateTime.Now.Year;
                //遍历dropdownlist
                foreach (ListItem item in this.droYear.Items)
                {
                    //显示当前年数
                    if (item.Text == year.ToString())
                    {
                        item.Selected = true;
                        break;
                    }
                }

                month = DateTime.Now.Month;
                foreach (ListItem item in this.droMonth.Items)
                {
                    if (item.Text == month.ToString())
                    {
                        item.Selected = true;
                        break;
                    }
                }

                if (DateTime.Now.Month == 1)
                {
                    year = DateTime.Now.Year - 1;
                    month = 12;
                }
                else
                {
                    year = DateTime.Now.Year;
                    month = DateTime.Now.Month - 1;
                }
                dataBind();
                BindChart();
                GetStatistics();
            }
        }


        /// <summary>
        /// Autor：曹鹏
        /// TIME：2011年4月11日13:52:43
        /// NOTE：合并单元格方法
        /// </summary>
        /// <param name="gv"></param>
        public void Combine(GridView gv)
        {
            int m;

            for (int i = 0; i < gv.Rows.Count; )//遍历
            {
                m = 0;//初始化变量

                for (int j = i + 1; j < gv.Rows.Count; j++)//内层循环
                {
                    if (gv.Rows[i].Cells[0].Text == gv.Rows[j].Cells[0].Text)//时间匹配
                    {

                        gv.Rows[j].Cells[0].Visible = false;//隐藏相同的单元格

                        m++;

                    }
                }

                gv.Rows[i].Cells[0].RowSpan = m + 1;//单元格合并

                i = i + m + 1;//给i赋值

            }
        }


        /// <summary>
        /// 数据绑定
        /// </summary>
        private void dataBind()
        {
            //add by 曹鹏 2011年5月11日21:10:45 月报表统计数据查询

            string where = " and AreaId like '" + AreaId.Substring(0, 4) + "%'";

            where += " and SortSource != 0 and SortSource != 1 ";

            where += " and daytime = " + this.droYear.SelectedItem.Text + this.droMonth.SelectedValue;

            //int t = 0;
            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
           
            dt = ie.StatisticsAreaByMonth(where);
            //显示相应的字段

            HZ.Supervise.Web.Function.AddColumn(dt, "WareHouseRate", "SortSource");//增加新列，主要目的是格式化显示字段

            HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "AddDistributeRate");

            gvwStatisticsArea.DataSource = dt;
            gvwStatisticsArea.DataBind();
            this.Combine(this.gvwStatisticsArea);

        }

        /// <summary>
        /// 绑定统计
        /// </summary>
        void GetStatistics()
        {
            double purchaseAmountTotal = 0;//采购总金额
            double DistributionAmountTotal = 0;
            double wareHouseAmountTotal = 0;//入库总金额
            for (int i = 0; i < gvwStatisticsArea.Rows.Count; i++)
            {
                purchaseAmountTotal += double.Parse(gvwStatisticsArea.Rows[i].Cells[2].Text);
                DistributionAmountTotal += double.Parse(gvwStatisticsArea.Rows[i].Cells[3].Text);
                wareHouseAmountTotal += double.Parse(gvwStatisticsArea.Rows[i].Cells[4].Text);
            }
            this.lblCount.Text = "采购总金额为:" + purchaseAmountTotal.ToString() + ";配送总金额为:" + DistributionAmountTotal.ToString() + ";入库总金额为:" + wareHouseAmountTotal.ToString();

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!this.droYear.SelectedValue.Equals("-1") && !this.droMonth.SelectedValue.Equals("-1"))
            {
                this.month = int.Parse(this.droMonth.SelectedValue);
                this.year = int.Parse(this.droYear.SelectedValue);
                dataBind();
                BindChart();
                GetStatistics();
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择月')</script>");
            }

        }

        private void BindChart()//modify by 曹鹏 2011-4-10 16:39:38 mschart控件数据绑定方法
        {

            //空为初始页
            if (dt.Rows.Count > 0)
            {
                this.divshow.Visible = true;
                string monch = "";//月

                #region 左图



                //***********************2010基本药物******************/
                Series series3 = new Series("2010基本药物");
                series3.MarkerStyle = MarkerStyle.Diamond;
                series3.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series3.BorderWidth = 1;
                series3.ShadowOffset = 1;
                series3.Label = "#VAL{F}";
                series3.Color = Color.FromArgb(74, 172, 197);
                //series3["DrawingStyle"] = "Cylinder";

                //***********************2010限价品种******************/
                Series series4 = new Series("2010限价品种");
                series4.MarkerStyle = MarkerStyle.Diamond;
                series4.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series4.BorderWidth = 1;
                series4.ShadowOffset = 1;
                series4.Color = Color.FromArgb(129, 101, 162);
                series4.Label = "#VAL{F}";
                //series4["DrawingStyle"] = "Cylinder";

                //***********************2010补充药品******************/
                Series series5 = new Series("2010补充药品");
                series5.MarkerStyle = MarkerStyle.Diamond;
                series5.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                series5.BorderWidth = 1;
                series5.ShadowOffset = 1;
                series5.Color = Color.FromArgb(192, 80, 78);
                series5.Label = "#VAL{F}";
               // series5["DrawingStyle"] = "Cylinder";
                #endregion

                //#region 右图


                ////***********************2010基本药物******************/
                //Series series8 = new Series("2010基本药物");
                //series8.MarkerStyle = MarkerStyle.Diamond;
                //series8.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series8.BorderWidth = 1;
                //series8.ShadowOffset = 1;
                //series8.Color = Color.DarkGray;
                //series8["DrawingStyle"] = "Cylinder";

                ////*/**********************2010限价品种******************/
                //Series series9 = new Series("2010限价品种");
                //series9.MarkerStyle = MarkerStyle.Diamond;
                //series9.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series9.BorderWidth = 1;
                //series9.ShadowOffset = 1;
                //series9.Color = Color.Blue;
                //series9["DrawingStyle"] = "Cylinder";

                ////***********************2010补充药品******************/
                //Series series10 = new Series("2010补充药品");
                //series10.MarkerStyle = MarkerStyle.Diamond;
                //series10.ChartType = (SeriesChartType)Enum.Parse(typeof(SeriesChartType), "Column", true);
                //series10.BorderWidth = 1;
                //series10.ShadowOffset = 1;
                //series10.Color = Color.Red;
                //series10["DrawingStyle"] = "Cylinder";
                //#endregion

                #region 数据绑定
                int s = 0;//用于计数

                string str = "";//用来存储时间中月份

                foreach (DataRow row in dt.Rows)
                {
                    monch = row["daytime"].ToString().Substring(4,2);//得到时间中的月份

                    if (!str.Contains(monch))//如果字符串中没有该月份，也就是说该月份未被查询过
                    {
                        str += monch + ",";//把月份加入字符串

                        if (row["SortSource"].ToString().Equals("3"))
                        {
                            series3.Points.AddXY(monch + "月", Convert.ToDouble(row["PurchaseAmountTotal"].ToString()) / 10000);
                            series3.Points[s].ToolTip = (Convert.ToDouble(row["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                            //series8.Points.AddXY(monch + "月", Convert.ToDouble(row["PurchaseCountTotal"].ToString()));
                            //series8.Points[s].ToolTip = (Convert.ToDouble(row["PurchaseCountTotal"].ToString())).ToString();


                            series4.Points.AddXY(monch + "月", 0);
                            series5.Points.AddXY(monch + "月", 0);

                            //series8.Points.AddXY(monch + "月", 0);
                            //series10.Points.AddXY(monch + "月", 0);
                        }
                        else if (row["SortSource"].ToString().Equals("5"))
                        {
                            series4.Points.AddXY(monch + "月", Convert.ToDouble(row["PurchaseAmountTotal"].ToString()) / 10000);
                            series4.Points[s].ToolTip = (Convert.ToDouble(row["PurchaseAmountTotal"].ToString()) / 10000).ToString();
                            //series9.Points.AddXY(monch + "月", Convert.ToDouble(row["PurchaseCountTotal"].ToString()));
                            //series9.Points[s].ToolTip = (Convert.ToDouble(row["PurchaseCountTotal"].ToString())).ToString();

                            series3.Points.AddXY(monch + "月", 0);
                            series5.Points.AddXY(monch + "月", 0);

                            //series8.Points.AddXY(monch + "月", 0);
                            //series10.Points.AddXY(monch + "月", 0);
                        }
                        else if (row["SortSource"].ToString().Equals("6"))
                        {
                            series5.Points.AddXY(monch + "月", Convert.ToDouble(row["PurchaseAmountTotal"].ToString()) / 10000);
                            series5.Points[s].ToolTip = row["PurchaseAmountTotal"].ToString();
                            //series10.Points.AddXY(monch + "月", Convert.ToDouble(row["PurchaseCountTotal"].ToString()));
                            //series10.Points[s].ToolTip = row["PurchaseCountTotal"].ToString();


                            series3.Points.AddXY(monch + "月", 0);
                            series4.Points.AddXY(monch + "月", 0);

                            //series8.Points.AddXY(monch + "月", 0);
                            //series9.Points.AddXY(monch + "月", 0);
                        }
                        s++;
                    }

                    else
                    {
                        if (row["SortSource"].ToString().Equals("3"))
                        {
                            series3.Points.RemoveAt(s - 1);
                            series3.Points.AddXY(monch + "月", (double.Parse(row["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                            series3.Points[s - 1].ToolTip = (double.Parse(row["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                        }
                        else if (row["SortSource"].ToString().Equals("5"))
                        {
                            series4.Points.RemoveAt(s - 1);
                            series4.Points.AddXY(monch + "月", (double.Parse(row["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                            series4.Points[s - 1].ToolTip = (double.Parse(row["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                            //series8.Points.RemoveAt(s - 1);
                            //series8.Points.AddXY(monch + "月", (double.Parse(row["PurchaseCountTotal"].ToString())).ToString());
                            //series8.Points[s - 1].ToolTip = (double.Parse(row["PurchaseCountTotal"].ToString())).ToString();
                        }
                        else if (row["SortSource"].ToString().Equals("6"))
                        {
                            series5.Points.RemoveAt(s - 1);
                            series5.Points.AddXY(monch + "月", (double.Parse(row["PurchaseAmountTotal"].ToString()) / 10000).ToString());
                            series5.Points[s - 1].ToolTip = (double.Parse(row["PurchaseAmountTotal"].ToString()) / 10000).ToString();

                            //series10.Points.RemoveAt(s - 1);
                            //series10.Points.AddXY(monch + "月", (double.Parse(row["PurchaseCountTotal"].ToString())).ToString());
                            //series10.Points[s - 1].ToolTip = (double.Parse(row["PurchaseCountTotal"].ToString())).ToString();
                        }
                    }
                }
                #endregion

                #region 数据绑定到图

                ChartPurchasing.Series.Add(series3);
                ChartPurchasing.Series["2010基本药物"].ChartArea = "ChartAreaPurchasing";

                ChartPurchasing.Series.Add(series4);
                ChartPurchasing.Series["2010限价品种"].ChartArea = "ChartAreaPurchasing";


                series5.IsValueShownAsLabel = false;
                ChartPurchasing.Series.Add(series5);
                ChartPurchasing.Series["2010补充药品"].ChartArea = "ChartAreaPurchasing";

                //series8.IsValueShownAsLabel = false;
                //ChartInStorage.Series.Add(series8);
                //ChartInStorage.Series["2010基本药物"].ChartArea = "ChartAreaStorage";

                //series9.IsValueShownAsLabel = false;
                //ChartInStorage.Series.Add(series9);
                //ChartInStorage.Series["2010限价品种"].ChartArea = "ChartAreaStorage";

                //series10.IsValueShownAsLabel = false;
                //ChartInStorage.Series.Add(series10);
                //ChartInStorage.Series["2010补充药品"].ChartArea = "ChartAreaStorage";
                #endregion

                #region 两张图片属性
                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.IsMarginVisible = true;
                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].Area3DStyle.Enable3D = false;

                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisX.IsMarginVisible = true;
                //ChartInStorage.ChartAreas["ChartAreaStorage"].Area3DStyle.Enable3D = false;

                foreach (Series item in this.ChartPurchasing.Series)
                {
                    item["PointWidth"] = "0.5";
                }

                //foreach (Series item in this.ChartInStorage.Series)
                //{
                //    item["PointWidth"] = "0.5";
                //}

                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.MajorGrid.Interval = 1;
                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisX.MajorGrid.Interval = 1;
                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.LineWidth = 1;
                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisY.LineWidth = 1;
                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisY.LineWidth = 1;

                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisY.Title = "金额（万元）";
                ChartPurchasing.ChartAreas["ChartAreaPurchasing"].AxisX.Title = "月";

                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisY.Title = "数量";
                //ChartInStorage.ChartAreas["ChartAreaStorage"].AxisX.Title = "周";



                #endregion
                lblnull.Visible = false;
            }
            else
            {
                this.divshow.Visible = false;
                //this.ChartInStorage.Visible = false;
                this.ChartPurchasing.Visible = false;
                lblnull.Visible = true;
            }

        }

        protected void droYear0_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (!this.droYear.SelectedValue.Equals("-1") && !this.droMonth.SelectedValue.Equals("-1"))
            {
                this.month = int.Parse(this.droMonth.SelectedValue);
                this.year = int.Parse(this.droYear.SelectedValue);
                dataBind();
                BindChart();
            }
            else
            {
                this.Page.RegisterStartupScript("", "<script>alert('请选择月')</script>");
            }

           
        }

        protected void btnExcel_Click(object sender, EventArgs e)
        {

            string where = " and AreaId like '" + AreaId.Substring(0, 4) + "%'";

            where += " and SortSource != 0 and SortSource != 1 ";

            where += " and daytime = " + this.droYear.SelectedItem.Text + this.droMonth.SelectedValue;

            HZ.Supervise.IBLL.IExecuteContext ie = HZ.Supervise.BLLFactory.ExecuteContext();
           
            dt = ie.StatisticsAreaByMonth(where);
            //显示相应的字段

            HZ.Supervise.Web.Function.AddColumn(dt, "WareHouseRate", "SortSource");//增加新列，主要目的是格式化显示字段

            HZ.Supervise.Web.Function.ChangeRate(dt, "DistributeRate", "AddDistributeRate");

            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, this.gvwStatisticsArea);

        }
    }
}
